home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / news / orbit / source / util.c < prev    next >
C/C++ Source or Header  |  2000-05-01  |  5KB  |  246 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     28.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <math.h>
  30.  
  31. void RotateAbout (double *vp, double *v, double *n, double theta)
  32. /*
  33.  *  Rotate the vector v about the vector n by theta radians, leaving
  34.  *  the rotated vector in vp.
  35.  */
  36. {
  37.   double X, Y, Z, x, y, z, sintheta, costheta, t1, t2, t3, t4, t5;
  38.   double t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17;
  39.   double t18, t19, a, b, c, d, e, f, g, h, i;
  40.   
  41.   X = v[0];
  42.   Y = v[1];
  43.   Z = v[2];
  44.  
  45.   x = n[0];
  46.   y = n[1];
  47.   z = n[2];
  48.  
  49.   sintheta = sin (theta);
  50.   costheta = cos (theta);
  51.  
  52.   t1 = x * x;
  53.   t2 = y * y;
  54.   t3 = z * z;
  55.  
  56.   t4 = 1.0 - t1;
  57.   t5 = 1.0 - t2;
  58.   t6 = 1.0 - t3;
  59.  
  60.   t7 = 1.0 - costheta;
  61.  
  62.   t8 = x * y;
  63.   t9 = x * z;
  64.   t10 = y * z;
  65.  
  66.   t11 = x * sintheta;
  67.   t12 = y * sintheta;
  68.   t13 = z * sintheta;
  69.  
  70.   t14 = t4 * costheta;
  71.   t15 = t5 * costheta;
  72.   t16 = t6 * costheta;
  73.  
  74.   t17 = t8 * t7;
  75.   t18 = t9 * t7;
  76.   t19 = t10 * t7;
  77.  
  78.   a = t1 + t14;
  79.   b = t17 + t13;
  80.   c = t18 - t12;
  81.  
  82.   d = t17 - t13;
  83.   e = t2 + t15;
  84.   f = t19 + t11;
  85.  
  86.   g = t18 + t12;
  87.   h = t19 - t11;
  88.   i = t3 + t16;
  89.  
  90.   vp[0] = X * a + Y * b + Z * c;
  91.   vp[1] = X * d + Y * e + Z * f;
  92.   vp[2] = X * g + Y * h + Z * i;
  93. }
  94.  
  95. void Normalize (double *v)
  96. {
  97.   double Mag(), vt[3];
  98.   Vdiv (vt, v, Mag(v));
  99.   Vset (v, vt);
  100. }
  101.  
  102. double Dotp (double *a, double *b)
  103. {
  104.   return (a[0]*b[0] + a[1]*b[1] + a[2]*b[2]);
  105. }
  106.  
  107. double Mag (double *a)
  108. {
  109.   return ((double) sqrt ((double)(a[0]*a[0] + a[1]*a[1] + a[2]*a[2])));
  110. }
  111.  
  112. double Mag2 (double *a)
  113. {
  114.   return (a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
  115. }
  116.  
  117. void Vadd (double *a, double *b, double *c)
  118. {
  119.   a[0] = b[0] + c[0];
  120.   a[1] = b[1] + c[1];
  121.   a[2] = b[2] + c[2];
  122. }
  123.  
  124. void Vsub (double *a, double *b, double *c)
  125. {
  126.   a[0] = b[0] - c[0];
  127.   a[1] = b[1] - c[1];
  128.   a[2] = b[2] - c[2];
  129. }
  130.  
  131. void Crossp (double *c, double *a, double *b)
  132. {
  133.   c[0] = a[1]*b[2] - a[2]*b[1];
  134.   c[1] = a[2]*b[0] - a[0]*b[2];
  135.   c[2] = a[0]*b[1] - a[1]*b[0];
  136. }
  137.  
  138. void Vdiv (double *a, double *b, double s)
  139. {
  140.   a[0] = b[0] / s;
  141.   a[1] = b[1] / s;
  142.   a[2] = b[2] / s;
  143. }
  144.  
  145. void Vmul (double *a, double *b, double s)
  146. {
  147.   a[0] = b[0] * s;
  148.   a[1] = b[1] * s;
  149.   a[2] = b[2] * s;
  150. }
  151.  
  152. void Vset (double *a, double *b)
  153. {
  154.   a[0] = b[0];
  155.   a[1] = b[1];
  156.   a[2] = b[2];
  157. }
  158.  
  159. double Dist2 (double x1, double y1, double z1, double x2, double y2, double z2)
  160. /*
  161.  *  Squared distance between two points
  162.  */
  163. {
  164.   double dx, dy, dz;
  165.  
  166.   dx = x1 - x2;
  167.   dy = y1 - y2;
  168.   dz = z1 - z2;
  169.  
  170.   return (dx*dx + dy*dy + dz*dz);
  171. }
  172.  
  173. double rnd (double r)
  174. /*
  175.  *  Random double on [0..r]
  176.  */
  177. {
  178.   int i;
  179.   double f;
  180.  
  181.   i = (rand() & 0xffff);
  182.   f = r * (((double)(i)) / 65536.0);
  183.  
  184.   return (f);
  185. }
  186.  
  187. void OutOfMemory()
  188. {
  189.   Log ("OutOfMemory: Out of memory!  Panicking!!");
  190.   FinishSound();
  191.   CloseLog();
  192.   exit (0);
  193. }
  194.  
  195. void Perp (double *v1, double *v)
  196. /*
  197.  *  Find a vector v1 perpendicular to non-zero vector v
  198.  *
  199.  *  Based on the idea that the dot product of perpendicular vectors is zero
  200.  */
  201. {
  202.   if ( (v[0] != 0.0) && (v[1] != 0.0) )
  203.   {
  204.     v1[0] = -1.0 / v[0];
  205.     v1[1] =  1.0 / v[1];
  206.     v1[2] = 0.0;
  207.   }
  208.   else if ( (v[1] != 0.0) && (v[2] != 0.0) )
  209.   {
  210.     v1[0] = 0.0;
  211.     v1[1] = -1.0 / v[1];
  212.     v1[2] =  1.0 / v[2];
  213.   }
  214.   else if ( (v[0] != 0.0) && (v[2] != 0.0) )
  215.   {
  216.     v1[0] = -1.0 / v[0];
  217.     v1[1] = 0.0;
  218.     v1[2] =  1.0 / v[2];
  219.   }
  220.   else if ( (v[0] == 0.0) && (v[1] == 0.0) && (v[2] != 0.0) )
  221.   {
  222.     v1[0] = 1.0;
  223.     v1[1] = 0.0;
  224.     v1[2] = 0.0;
  225.   }
  226.   else if ( (v[0] != 0.0) && (v[1] == 0.0) && (v[2] == 0.0) )
  227.   {
  228.     v1[0] = 0.0;
  229.     v1[1] = 1.0;
  230.     v1[2] = 0.0;
  231.   }
  232.   else if ( (v[0] == 0.0) && (v[1] != 0.0) && (v[2] == 0.0) )
  233.   {
  234.     v1[0] = 1.0;
  235.     v1[1] = 0.0;
  236.     v1[2] = 0.0;
  237.   }
  238.   else
  239.   {
  240.     /* Should never happen */
  241.     v1[0] = 1.0;
  242.     v1[1] = 0.0;
  243.     v1[2] = 0.0;
  244.   }
  245. }
  246.